/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: connectionengine.h.
*/

#ifndef CONNECTIONENGINE_H
#define CONNECTIONENGINE_H

#include <QObject>
#include <qbluetoothaddress.h>

//! Forward declarations.
class NfcAiwWrapper;
class BtServer;
class BtClient;

QTM_USE_NAMESPACE

//! Connection engine class.
/*!
  This class works as an engine for creating a Bluetooth connection between LocationApp
  applications in different devices. Engine class always starts an RfCommServer to listen
  for new connections on a local address, i.e. when the application is launched, it is ready
  to establish a connection to a client device.

  The engine class can request remote Bluetooth connection info via an NFC connection and
  connect as a Bluetooth client to another device by using this information.

  After the Bluetooth connection is successfully established, two devices are able
  to share messages with each other.
*/
class ConnectionEngine : public QObject
{
    Q_OBJECT

public:
    //! Constructor.
    /*!
      Constructor starts the Bluetooth server to listen for new connections to
      its local address and selected port.
    */
    explicit ConnectionEngine(QObject *parent = 0);

    //! Destructor.
    virtual ~ConnectionEngine();

public Q_SLOTS:
    //! Opens the Bluetooth connection as a client.
    /*!
      Starts the NFC request to get the address of the remote Bluetooth client.
      When the address is received a Bluetooth connection to that address and
      selected port is established.
     */
    void openConnection();

    //! Closes the Bluetooth connection.
    void closeConnection();

    //! Writes data to the Bluetooth socket.
    /*!
      \param sharedData A byte array which contains the data to be sent.
     */
    void shareData(const QByteArray &sharedData);

private Q_SLOTS:
    //! Data read from the Bluetooth socket.
    /*!
      \param message A byte array which contains the received message.
     */
    void newMessage(const QByteArray &message);

    //! Reads connection info and creates a BtClient.
    /*!
      \param connectionInfo A byte array which contains the remote device's
             Bluetooth connection information.
      \sa openConnection()
     */
    void readConnectionInfo(const QByteArray &connectionInfo);

    //! Bluetooth client successfully connected to remote server.
    /*!
      \param remoteName The name of the remote device.
      \sa disconnected()
     */
    void handleConnected(const QString &remoteName);

    //! Client disconnected from remote server.
    /*!
      \sa handleConnected()
     */
    void disconnected();

    //! New client connected to BtServer.
    /*!
      \param clientName The name of the remote device which created the connection.
      \sa clientDisconnected()
     */
    void clientConnected(const QString &clientName);

    //! Client disconnected from BtServer.
    /*!
      \param clientName The name of the client which was disconnected.
      \sa clientConnected()
     */
    void clientDisconnected(const QString &clientName);

Q_SIGNALS:
    //! Signalled when new message is received.
    /*!
      \param message A byte array which contains data from remote device.
     */
    void messageReceived(const QByteArray &message);

    //! Signalled when a Bluetooth connection is established.
    /*!
      \param remoteName The name of the remote device.
     */
    void newConnection(const QString &remoteName);

    //! Signalled when a Bluetooth connection is closed by the client.
    /*!
      \param remoteName The name of the remote device.
     */
    void connectionClosed(const QString &remoteName);

private:
    //! Writes address from QByteArray to format XXXXXXXXXXXX, where X is a hexadecimal digit.
    QString formatAddress( const QByteArray &address );

private:
    QString m_deviceName; //!< Name of the remote device.

    QBluetoothAddress m_deviceAddress; //!< Address of the remote device.

    BtClient *m_client; //!< Bluetooth client class.

    BtServer *m_server; //!< Bluetooth server class.

    NfcAiwWrapper *m_aiwWrapper; //!< AIW wrapper class to get remote Bluetooth
                                 //!< info using NFC.

};

#endif // CONNECTIONENGINE_H

